<?php
namespace Home\Controller;
use Common\Controller\UserController;
class IndexController extends UserController {
    public function index(){
        $this->display();       
    }
    public function login(){
        if (IS_POST) {
            $username = I('post.username');
            $password = md5(I('post.password'));
            $ComuserModel=M('Comuser');
            $user = $ComuserModel->where("username='".$username."'")->find();
            if($user['password']==$password){
                session('id',$user['id']);
                session('username',$user['username']);
                // print_r(session());
                header('Location:'.U('index/index'));   
            }else{
                $this->error('用户名或密码错误');
            }
        }
        $this->display();       
    }
    public function regist(){
        if (IS_POST) {
            $ComuserModel=D('Comuser');
            $pass=md5(I('post.password',0));
            $passd=md5(I('post.passworden',0));
            if ($pass!=$passd){
                echo $this->error('两次密码输入不一样');
                exit;
            }
            $data=array(
                'username'=>I('post.username',0),
                'password'=>md5(I('post.password',0)),
                'email'=>I('post.email',0),
                'add_time'=>date('Y-m-d H:i:s',time()), 
                );
            $comuser=D('Comuser')->where("username='".I('post.username')."'")->find();
            if (empty($comuser)) {
                if ($ComuserModel->create($data)) {
                    $ComuserModel->add();
                    header('Location:'.U('login'));
                }else{
                    $this->error($ComuserModel->getError());
                }
            }else{
                $this->error('用户已存在');
            }
        }
            $this->display();     
    }
    public function loginout(){
        session(null);
        header('Location:'.U('index/index'));
    }
    public function product(){
        if (I('get.pid')) {
            $pid=I('get.pid');
            $MerchandiseModel = M('Merchandise');
            $arr=M('Merchandise')->field('fm_merchandise.*,(select fm_photos.photos from fm_photos where fm_merchandise.id=fm_photos.merchandise_id limit 1) as photos')->join('left join fm_merchandise_category on fm_merchandise.id=fm_merchandise_category.merchandise_id')->where('fm_merchandise_category.category_id='.$pid)->select();
            $cate= M('Merchandise_category')->join('left join fm_category on fm_category.id=fm_merchandise_category.category_id')->where('category_id='.$pid)->select();
        }else{
          $MerchandiseModel = M('Merchandise');
          $arr= M('Merchandise')->field('fm_merchandise.*,(select fm_photos.photos from fm_photos where fm_merchandise.id=fm_photos.merchandise_id limit 1) as photos')->select(); 
        }
        $this->assign('cate',$cate);      
        $this->assign('product',$arr);
    	$this->display();
    }

    public function product_details(){
        if (I('get.id')) {
            $id=I('get.id');
            $MerchandiseModel = M('Merchandise');
            $arr= M('Merchandise')->join('left join fm_merchandise_category on fm_merchandise.id=fm_merchandise_category.merchandise_id')->where('fm_merchandise_category.merchandise_id='.$id)->find();
            $cate= M('Merchandise_category')->join('left join fm_category on fm_category.id=fm_merchandise_category.category_id')->where('merchandise_id='.$id)->select();
            $photos=M('Photos')->where("merchandise_id=".$id)->select();        
        }

        $this->assign('cate',$cate);
        $this->assign('product',$arr);
        $this->assign('photos',$photos);
        $this->display();
    }
}